package cn.g31010.sip.service.impl;

import java.util.List;

import org.apache.log4j.Logger;

import cn.g31010.sip.pojo.Room;
import cn.g31010.sip.pojo.User;
import cn.g31010.sip.service.RoomManageService;
import cn.g31010.sip.util.DB;
import cn.g31010.sip.util.IDUtil;

import com.db4o.ObjectSet;
import com.db4o.query.Predicate;
import com.gqf.common.util.DynamicResource;

public class RoomManageServiceImpl implements RoomManageService {

	private DB db = DB.getDB(DynamicResource.getString("appchat", "db.file"));
	private Logger logger = Logger.getLogger(UserManageServiceImpl.class);
	private IDUtil rid =IDUtil.getIntance("cn.g31010.sip.service.impl.rid");
	
	
	public RoomManageServiceImpl(){
		rid.setPattern("RM0000");
	}
	public Room addRoom(Room room) {
		room.setId(rid.genId());
		db.add(room);
		
		return room;
	}

	public Room findRoomById(String id) {
		final String rid = id;
		Predicate<Room> filter = new Predicate<Room>() {
			public boolean match(Room candidate) {
				logger.debug("match("+candidate+")");
				return candidate.getId()==rid||rid==null?candidate.getId().equals(rid):rid.equals(candidate.getId());
			}
		};
		ObjectSet<Room> rs = db.find(filter);
		if (rs.size() > 0) {
			return rs.get(0);
		}
		return null;
	}

	public List<Room> findRoomByName(String name) {
		final String rname = name;
		Predicate<Room> filter = new Predicate<Room>() {
			public boolean match(Room candidate) {
				logger.debug("match("+candidate+")");
				return candidate.getName()==rname||rname==null?candidate.getName().equals(rname):rname.equals(candidate.getName());
			}
		};
		ObjectSet<Room> rs = db.find(filter);
		return rs.subList(0, rs.size());
	}
	
	public List<Room> findRoobByClass(String csid){
		final String cid = csid;
		Predicate<Room> filter = new Predicate<Room>() {
			public boolean match(Room candidate) {
				logger.debug("match("+candidate+")");
				return candidate.getClasses().getId()==cid||cid==null?candidate.getClasses().getId().equals(cid):cid.equals(candidate.getClasses().getId());
			}
		};
		ObjectSet<Room> rs = db.find(filter);
		return rs.subList(0, rs.size());
	}

	public List<Room> getAllRooms() {
		ObjectSet<Room> rs = db.find(Room.class);
		return rs.subList(0, rs.size());
	}

	public Room removeRoom(String id) {
		Room room=findRoomById(id);
		if(room!=null){
			db.delete(room);
			return room;
		}
		return null;
	}

	public Room setRoom(Room room) {
		Room r=findRoomById(room.getId());
		if(r!=null){
			room.setOwner(r.getOwner());
			room.setAdmins(r.getAdmins());
			room.setBlackMemebers(r.getBlackMemebers());
			room.setWhiteMembers(r.getWhiteMembers());
			db.update(room);
			return room;
		}
		return null;
	}
	
	public User findUserById(String id) {
		final String uid = id;
		Predicate<User> filter = new Predicate<User>() {
			public boolean match(User candidate) {
				logger.debug("match("+candidate+")");
				return candidate.getId()==uid||uid==null?candidate.getId().equals(uid):uid.equals(candidate.getId());
			}
		};
		ObjectSet<User> rs = db.find(filter);
		if (rs.size() > 0) {
			return rs.get(0);
		}

		return null;
	}

	/* (non-Javadoc)
	 * @see cn.g31010.sip.service.RoomManageService#addAdmin(java.lang.String, java.lang.String)
	 */
	public Room addAdmin(String id, String uid) {
		User user = findUserById(uid);
		if(user!=null){
			Room room = findRoomById(id);
			if(room!=null){
				room.getAdmins().add(user);
				db.update(room);
				return room;
			}
		}
		
		return null;
	}

	/* (non-Javadoc)
	 * @see cn.g31010.sip.service.RoomManageService#addBlack(java.lang.String, java.lang.String)
	 */
	public Room addBlack(String id, String uid) {
		User user = findUserById(uid);
		if(user!=null){
			Room room = findRoomById(id);
			if(room!=null){
				room.getBlackMemebers().add(user);
				db.update(room);
				return room;
			}
		}
		
		return null;
	}

	/* (non-Javadoc)
	 * @see cn.g31010.sip.service.RoomManageService#addWhite(java.lang.String, java.lang.String)
	 */
	public Room addWhite(String id, String uid) {
		User user = findUserById(uid);
		if(user!=null){
			Room room = findRoomById(id);
			if(room!=null){
				room.getWhiteMembers().add(user);
				db.update(room);
				return room;
			}
		}
		
		return null;
	}

	/* (non-Javadoc)
	 * @see cn.g31010.sip.service.RoomManageService#removeAdmin(java.lang.String, java.lang.String)
	 */
	public Room removeAdmin(String id, String uid) {
		User user = findUserById(uid);
		if(user!=null){
			Room room = findRoomById(id);
			if(room!=null){
				room.getAdmins().remove(user);
				db.update(room);
				return room;
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see cn.g31010.sip.service.RoomManageService#removeBlack(java.lang.String, java.lang.String)
	 */
	public Room removeBlack(String id, String uid) {
		User user = findUserById(uid);
		if(user!=null){
			Room room = findRoomById(id);
			if(room!=null){
				room.getBlackMemebers().remove(user);
				db.update(room);
				return room;
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see cn.g31010.sip.service.RoomManageService#removeOwner(java.lang.String, java.lang.String)
	 */
	public Room removeOwner(String id, String uid) {
		User user = findUserById(uid);
		if(user!=null){
			Room room = findRoomById(id);
			if(room!=null&&user.getId().equals(room.getOwner().getId())){
				room.setOwner(null);
				db.update(room);
				return room;
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see cn.g31010.sip.service.RoomManageService#removeWhite(java.lang.String, java.lang.String)
	 */
	public Room removeWhite(String id, String uid) {
		User user = findUserById(uid);
		if(user!=null){
			Room room = findRoomById(id);
			if(room!=null){
				room.getWhiteMembers().remove(user);
				db.update(room);
				return room;
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see cn.g31010.sip.service.RoomManageService#setOwner(java.lang.String, java.lang.String)
	 */
	public Room setOwner(String id, String uid) {
		User user = findUserById(uid);
		if(user!=null){
			Room room = findRoomById(id);
			if(room!=null){
				room.setOwner(user);
				db.update(room);
				return room;
			}
		}
		return null;
	}

}
